package com.jhg.drgclinicalpathway.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jhg.drgclinicalpathway.dao.*;
import com.jhg.drgclinicalpathway.domain.JC参数设置;
import com.jhg.drgclinicalpathway.domain.RpnMsg;
import com.jhg.drgclinicalpathway.domain.VDCP_入组结算明细;
import com.jhg.drgclinicalpathway.domain.V病组标杆维护;
import com.jhg.drgclinicalpathway.service.ReportWordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("report")
@Slf4j(topic = "ReportController")
public class ReportController {

    @Autowired
    CostAnalysisDao costAnalysisDao;

    @GetMapping("cost-ls")
    public RpnMsg costLs(@RequestParam(defaultValue = "") String stime,
                         @RequestParam(defaultValue = "") String etime,
                         @RequestParam(defaultValue = "b.分组编码,b.分组名称") String grp,
                         @RequestParam(defaultValue = "10") Integer size,
                         @RequestParam(defaultValue = "病例数 DESC") String order,
                         @RequestParam(defaultValue = "1=1") String w,
                         @RequestParam(defaultValue = "出院日期") String t,
                         @RequestParam(defaultValue = "1") String 标杆来源) {

        if (grp.indexOf("医生") > 0) {
            w += " and 标杆来源=" + 标杆来源.replace("{\"isTrusted\":true}", "1");
        }
        List<Map<String, Object>> ls = costAnalysisDao.ls(grp, stime, etime, order, w, size, t);
        return RpnMsg.SUCCESS(ls);
    }


    @Autowired
    CommonDao commonDao;

    @Autowired
    VDCP_入组结算明细Dao 结算明细Dao;

    @GetMapping("medc-top")
    public RpnMsg medcTop(String stime,
                          String etime,
                          @RequestParam(defaultValue = "") String ks,
                          @RequestParam(defaultValue = "") String code,
                          @RequestParam(defaultValue = "") String doc,
                          @RequestParam(defaultValue = "出院日期") String t,
                          @RequestParam(defaultValue = "药品") String pt,
                          @RequestParam(defaultValue = "10") Integer ts,
                          @RequestParam(defaultValue = "0") Integer ljid
    ) {
        List<Map<String, Object>> list = costAnalysisDao.获取项目Top(stime, etime, ks, doc, code, t, pt, ts, ljid);
        List<String> 名称Ls = new ArrayList<>();
        for (Map<String, Object> map : list) {
            String 名称 = map.getOrDefault("名称", "").toString();
            if (StringUtils.isEmpty(名称)) continue;
            名称Ls.add(名称);
        }

        if (名称Ls.size() == 0) return RpnMsg.SUCCESS(list);

        List<Map<String, Object>> 药品说明Map = commonDao.药品说明(名称Ls);
        for (Map<String, Object> map : list) {
            String 名称 = map.getOrDefault("名称", "").toString();
            if (StringUtils.isEmpty(名称)) continue;
            for (Map<String, Object> map1 : 药品说明Map) {
                String 名称1 = map1.getOrDefault("名称", "").toString();

                if (名称.equals(名称1)) {
                    String 适用范围 = map1.getOrDefault("适用范围", "").toString();
                    map.put("药品说明", 适用范围);
                }
            }
        }

        return RpnMsg.SUCCESS(list);
    }

    /**
     * PRO_病人路径评估_项目对比增删
     *
     * @param 操作方式
     * @param 版本ID
     * @param 项目ID
     * @return
     */
    @PostMapping("proc-cmp-opt")
    public RpnMsg proc_cmp_opt(String 操作方式, String 版本ID, String 项目ID) {

        try {
            costAnalysisDao.PRO_病人路径评估_项目对比增删(操作方式, 版本ID, 项目ID);
            return RpnMsg.SUCCESS();
        } catch (Exception ex) {
            String msg = ex.getMessage();
            if (ex.getCause() != null) {
                msg = ex.getCause().getMessage();
            }
            return RpnMsg.SUCCESS(msg);
        }
    }

    @Autowired
    ReportWordService wordService;

    @PostMapping("word")
    public void lineWayReport(
            @RequestBody Map<String, String> map,
            HttpServletRequest req, HttpServletResponse rep) throws Exception {

        wordService.gener(map, req, rep);
    }

    @Autowired
    LJ临床路径版本Dao lj临床路径版本Dao;

    @Autowired
    JC参数设置Dao jc参数设置Dao;

    @GetMapping("pro-cmp")
    public RpnMsg 临床路径对比(@RequestParam(defaultValue = "stime") String stime,
                               @RequestParam(defaultValue = "") String etime,
                               @RequestParam(defaultValue = "") String zyys,
                               @RequestParam(defaultValue = "") String xmlx,
                               @RequestParam(defaultValue = "") String tjlb,
                               @RequestParam(defaultValue = "") String tjms
    ) {
//        List<String> dos= new ArrayList<>();
//        if(!StringUtils.isEmpty(docs)){
//            dos=Arrays.asList(docs.split(","));
//        }
        double r = 0.3;
        QueryWrapper<JC参数设置> queryWrapper = new QueryWrapper<JC参数设置>();
        queryWrapper.eq("名称", "项目对比占比");
        List<JC参数设置> jc参数设置s = jc参数设置Dao.selectList(queryWrapper);
        if (jc参数设置s.size() > 0) {
            try {
                r = Double.parseDouble(jc参数设置s.get(0).get参数值());
            } catch (Exception ex) {
            }
        }
        List<Map<String, Object>> 项目对比 = lj临床路径版本Dao.项目对比2(stime, etime, zyys, xmlx, tjlb, tjms, r);
        return RpnMsg.SUCCESS(项目对比);
    }

    /**
     * 1.病种趋势分析表
     *
     * @return List<String>
     */
    @PostMapping("rpt-bzqs")
    public RpnMsg rpt_bzqs(@RequestBody Map<String, Object> map) {
        String kssj = (String) map.get("kssj");
        String jssj = (String) map.get("jssj");
        String keys = (String) map.get("keywords");
        List<Map<String, Object>> rs = null;
        if (!"null".equals(keys) && "".equals(keys) && !"".equals(map.get("size").toString())) {
            int size = Integer.parseInt(map.get("size").toString());
            String yk = (String) map.get("yk");
            rs = commonDao.getBzqsHzReport(kssj, jssj, size, yk);
        } else {
            rs = commonDao.getBzqsReport(kssj, jssj, keys);
        }
        return RpnMsg.SUCCESS(rs);
    }

    /**
     * 2.在院病人入径情况
     *
     * @return
     */
    @PostMapping("rpt-rjqk-zy")
    public RpnMsg rpt_Zybr_Rjqk(@RequestBody Map<String, Object> map) {
        String ksmc = "";// (String) map.get("ksmc");
        List<Map<String, Object>> rs = commonDao.getZybrRjqkReport(ksmc);
        return RpnMsg.SUCCESS(rs);
    }

    /**
     * 模板使用率统计
     *
     * @return
     */
    @PostMapping("rpt-mbsy")
    public RpnMsg rpt_Mbsyqk(@RequestBody Map<String, Object> map) {
        String kssj = (String) map.get("kssj");
        String jssj = (String) map.get("jssj");
        String mbid = (String) map.get("mbid");
        List<Map<String, Object>> rs = commonDao.getMbsyReport(kssj, jssj, mbid);
        return RpnMsg.SUCCESS(rs);
    }

    /**
     * Drg入组明细
     *
     * @param map
     * @return
     */
    @PostMapping("rpt-rzmx")
    public RpnMsg rpt_Rzmx(@RequestBody Map<String, Object> map) {
        String kssj = (String) map.get("kssj");
        String jssj = (String) map.get("jssj");
        String ksmc = (String) map.get("ksmc");
        int lb = (int) map.get("lb");
        String keys = (String) map.get("keys");
        int page = (int) map.get("page");
        int size = (int) map.get("size");
        if (lb == 1) {
            Page<VDCP_入组结算明细> pageInfo = new Page<VDCP_入组结算明细>(page, size);
            QueryWrapper<VDCP_入组结算明细> queryWrapper = new QueryWrapper<VDCP_入组结算明细>();
            queryWrapper.between("结算时间", kssj, jssj);
            if (!"".equals(ksmc)) {
                queryWrapper.eq("出院科室名称", ksmc);
            }
            if (!StringUtils.isEmpty(keys)) {
                queryWrapper.and(
                        wrapper ->
                                wrapper.like("DRG编码", keys)
                                        .or().like("DRG名称", keys)
                                        .or().like("姓名", keys)
                                        .or().like("住院号", keys)
                );
            }
            if (map.get("profit") != null) {
                if ("2".equals(map.get("profit").toString())) {
                    queryWrapper.gt("盈亏状况", 0);
                } else if ("3".equals(map.get("profit").toString())) {
                    queryWrapper.lt("盈亏状况", 0);
                }
            }
            IPage<VDCP_入组结算明细> ls = 结算明细Dao.selectPage(pageInfo, queryWrapper);
            return RpnMsg.SUCCESS(ls);
        } else if (lb == 2) {
            Page<VDCP_入组结算明细> pagehzInfo = new Page<VDCP_入组结算明细>(page, size);
            QueryWrapper<VDCP_入组结算明细> queryWrapper = new QueryWrapper<VDCP_入组结算明细>();
            queryWrapper.select("出院科室名称 as 科室名称,DRG编码,DRG名称,DRG权重,count(1) 人次,convert(NUMERIC(18,2),avg(住院总费用)) 住院例均费用," +
                    "sum(DDRG标准支付金额) DRG标准支付总额,convert(NUMERIC(18,2),sum(医保基金DRG支付)) 医保基金DRG支付总额,sum(盈亏状况) as 盈亏");
            queryWrapper.between("结算时间", kssj, jssj);
            queryWrapper.eq("入组状态", 1);
            if (!"".equals(ksmc)) {
                queryWrapper.eq("出院科室名称", ksmc);
            }
            if (!StringUtils.isEmpty(keys)) {
                queryWrapper.and(
                        wrapper ->
                                wrapper.like("DRG编码", keys)
                                        .or().like("DRG名称", keys)
                );
            }
            queryWrapper.groupBy("出院科室名称 ,DRG编码,DRG名称,DRG权重");
            if (map.get("profit") != null) {
                if ("2".equals(map.get("profit").toString())) {
                    queryWrapper.having("sum(盈亏状况)>0");
                } else if ("3".equals(map.get("profit").toString())) {
                    queryWrapper.having("sum(盈亏状况)<0");
                }
            }
            IPage<Map<String, Object>> ls = 结算明细Dao.selectMapsPage(pagehzInfo, queryWrapper);
            return RpnMsg.SUCCESS(ls);
        } else if (lb == 3) {
            List<Map<String, Object>> lsm = commonDao.getRpt_Rzmx(kssj, jssj);
            return RpnMsg.SUCCESS(lsm);
        } else {
            return RpnMsg.FAIL("获取失败");
        }
    }

    @PostMapping("ls-bzbm")
    public RpnMsg getDrgmc(@RequestBody Map<String, Object> map) {
        String key = map.get("key").toString();
        if (!"".equals(key)) {
            List<Map<String, Object>> rs = commonDao.surchDrgGroupByBm(key);
            return RpnMsg.SUCCESS(rs);
        } else {
            return RpnMsg.SUCCESS("");
        }
    }

    /***
     * DRGS入组情况分析
     * @param map
     * @return
     */
    @PostMapping("rpt-rzqkfx")
    public RpnMsg rpt_BRrzqkfx(@RequestBody Map<String, Object> map) {
        int lb = (int) map.get("lb");
        String kssj = (String) map.get("kssj");
        String jssj = (String) map.get("jssj");
        List<String> dept = new ArrayList<String>();
        if (lb == 1) {
            if (map.get("dept") != null && !"".equals(map.get("dept").toString())) {
                dept = (ArrayList<String>) map.get("dept");
            }
            // System.out.println("123");
            return RpnMsg.SUCCESS(commonDao.getRpt_RZQKHZ(kssj, jssj, dept));
        } else if (lb == 2) {
            if (map.get("dept") != null && !"".equals(map.get("dept").toString())) {
                dept = (ArrayList<String>) map.get("dept");
            }
            return RpnMsg.SUCCESS(commonDao.getRpt_RZQKFk(kssj, jssj, dept));
        } else if (lb == 3) {
            int size = Integer.parseInt(map.get("size").toString());
            return RpnMsg.SUCCESS(commonDao.getRpt_RZQKFBZ(kssj, jssj, size, map.get("dept").toString()));
        }
        return RpnMsg.SUCCESS("");
    }

    @PostMapping("rpt-zyzb")
    public RpnMsg rpt_ZYTJ(@RequestBody Map<String, Object> map) {
        String kssj = (String) map.get("kssj");
        String jssj = (String) map.get("jssj");
        int zyzb = (int) map.get("zyzb");
        return RpnMsg.SUCCESS(commonDao.getRpt_Zyzbtj(kssj, jssj, zyzb));
    }

    @GetMapping("wksstj")
    public RpnMsg t(@RequestParam(defaultValue = "stime") String stime,
                    @RequestParam(defaultValue = "") String etime) {
        List<Map<String, Object>> 外科手术室开单统计 = commonDao.外科手术室开单统计(stime, etime);
        return RpnMsg.SUCCESS(外科手术室开单统计);
    }

    /**
     *
     * @param stime
     * @param etime
     * @param vs
     * @return
     */
    @GetMapping("zdkfxmfx")
    public RpnMsg zdkfxmfx(
            @RequestParam(defaultValue = "") String kssj,
            @RequestParam(defaultValue = "") String jssj,
            @RequestParam(defaultValue = "",name = "mbid") String vs,
            @RequestParam(defaultValue = "重点") String type
            ){
        List<Map<String, Object>> maps=null;
        if(type.equals("重点")) maps = commonDao.P_重点控费项目分析(kssj, jssj, vs);
        else maps = commonDao.P_经常漏查的项目分析(kssj, jssj, vs);
        return RpnMsg.SUCCESS(maps);
    }
}